package top.jandmla.shop.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jandmla.shop.bean.Discount;
import top.jandmla.shop.bean.pojo.QueryDto;
import top.jandmla.shop.dao.DiscountMapper;
import top.jandmla.shop.service.DiscountService;
import top.jandmla.shop.utils.result.BaseResult;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 */
@Service
public class DiscountServiceImpl extends ServiceImpl<DiscountMapper, Discount> implements DiscountService {

    @Resource
    private DiscountMapper mapper;

    @Override
    public BaseResult<IPage<Discount>> selectPage(QueryDto<Discount> dto) {
        QueryWrapper<Discount> qw = new QueryWrapper<>();
        if (dto != null){
            Discount discount = dto.getQuery();
            if (discount != null){
                if (discount.getId() > 0)
                    qw.eq("id",discount.getId());
                if (StringUtils.isNotBlank(discount.getDiscountname()))
                    qw.eq("discountName",discount.getDiscountname());
                if (StringUtils.isNotBlank(discount.getDiscountintro()))
                    qw.like("discountIntro",discount.getDiscountintro());
                if (discount.getDiscount() > 0.0)
                    qw.eq("discount",discount.getDiscount());
            }
            return BaseResult.success(mapper.selectPage(dto.getPage(),qw));
        }
        return BaseResult.fail(5000,"请求参数错误！");
    }
}
